package DEEPERsource.DEEPERsource.source.jung.entities;

import java.util.Collection;
import java.util.List;

import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.impl.SparseGraph;
import exceptions.GraphException;
import graph.Keys;

/**
 * Special kind of graph for storing dependency trees.
 * It is a directed graph with possible parralel arcs.
 * @author tfayruzo
 *
 */
public class UndirectedGraph extends SparseGraph
	implements edu.uci.ics.jung.graph.UndirectedGraph, Keys {

	/**
	 * Creates an instance of a sparse directed graph.
	 */
	public UndirectedGraph() {
		super();
        @SuppressWarnings("unused")
		Collection edge_predicates = 
            getEdgeConstraints();
	}
	
	public Vertex findVertex(Vertex v){
		for(Object vert : mVertices){
			Vertex _vert = (Vertex)vert;
			if(_vert.getUserDatum(INDEX).equals(v.getUserDatum(INDEX))){
				return _vert;
			}
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Edge> getPath(Vertex v1, Vertex v2) throws GraphException{
		/*if(! (v1 instanceof DependencyVertex) ||
				! (v2 instanceof DependencyVertex)){
				return null;
			}*/
			Vertex _v1 = findVertex(v1);
			Vertex _v2 = findVertex(v2);
			if(_v1 == null || _v2 == null){
				throw new RuntimeException("One of the vertices does not exist in the graph");
			}
			DijkstraShortestPath path = new DijkstraShortestPath(this);
			List<Edge> edges = path.getPath(_v1, _v2);
			if(edges.size() == 0 && _v1 != v2)
				throw new GraphException("Could not find path from root ("+_v1.getUserDatum(INDEX)+") to protein ("+_v2.getUserDatum(INDEX)+")");
			return edges;
	}
}
